﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSunny.Admin.Controls
{
    public partial class EditSlideShow : System.Web.UI.UserControl
    {
        public string imageslide;
        public string nameslide;
        public string link;
        public string title;
        public int slideid;
        public string filePathSlide;
        protected void Page_Load(object sender, EventArgs e)
        {
            slideid = Convert.ToInt32(Request.QueryString["idslide"]);
            DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Slider", slideid.ToString());
            imageslide = dt.Rows[0]["Slider_Image"].ToString();
            link = dt.Rows[0]["Slider_Link"].ToString();
            title = dt.Rows[0]["Slider_Title"].ToString();
            nameslide = dt.Rows[0]["Slider_Alt"].ToString();
            txtDetail.Text = dt.Rows[0]["Slider_Detail"].ToString();
            txtLink.Text = link;
            txtTitle.Text = title;
            txtName.Text = nameslide;

        }

        protected void btnUpdateSlide_Click(object sender, EventArgs e)
        {
            if (txtLink.Text != "")
            {
                if (txtTitle.Text != "")
                {
                    if (fulImageSlide.HasFile)
                    {
                        if (LIB.Validate.CheckFileType(fulImageSlide.FileName))
                        {
                            string linkimage = fulImageSlide.FileName;
                            LIB.Validate saveimage = new LIB.Validate();
                            saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Slide-Show/"), linkimage,800, fulImageSlide.PostedFile.InputStream);
                            filePathSlide = "/Resources/Images/Slide-Show/" + fulImageSlide.FileName;

                            DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[5];
                            cl[0] = new DAL.DHelpers.Column("Slider_Alt", txtName.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[1] = new DAL.DHelpers.Column("Slider_Link", txtLink.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[2] = new DAL.DHelpers.Column("Slider_Title", txtTitle.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[3] = new DAL.DHelpers.Column("Slider_Image", filePathSlide, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[4] = new DAL.DHelpers.Column("Slider_Detail", txtDetail.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                            string colWhere  = "Slider_ID";

                            string colValue =  slideid.ToString();

                            DAL.DCollector.DBCollector.Instance.StoredUpdate("tbl_Slider", cl, colWhere, colValue);
                            Response.Redirect("/Admin/Index.aspx?panel=5&sub=0");
                        }
                        else
                        {
                            Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                        }

                    }
                    else
                    {
                        DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[4];
                        cl[0] = new DAL.DHelpers.Column("Slider_Alt", txtName.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                        cl[1] = new DAL.DHelpers.Column("Slider_Link", txtLink.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                        cl[2] = new DAL.DHelpers.Column("Slider_Title", txtTitle.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                        
                        cl[3] = new DAL.DHelpers.Column("Slider_Detail", txtDetail.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);


                        string colWhere = "Slider_ID";

                        string colValue = slideid.ToString();

                        DAL.DCollector.DBCollector.Instance.StoredUpdate("tbl_Slider", cl, colWhere, colValue);

                        Response.Redirect("/Admin/Index.aspx?panel=5&sub=0");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Vui lòng nhập tên tiêu đề slideshow...')</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('Vui lòng nhập link liên kết...')</script>");
            }

        }
    }
}